﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Text;
using Framework;

namespace Client
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.CancelKeyPress += (sender, e) =>
            {
                e.Cancel = true;
                //Ctrl + C关闭企图被取消，

            };
            Console.WriteLine("****************企业站基础管理*****************");
            Console.WriteLine("*******************操作说明********************");
            Console.WriteLine("*                                             *");
            Console.WriteLine("***********************************************");
            while (true)
            {
            Input:
                Console.Write("Enterprise>");
                var commend = Console.ReadLine();
                commend = (commend ?? string.Empty).ToLower();
                if (string.IsNullOrEmpty(commend)) goto Input;
                switch (commend)
                {
                    case "con":
                        {
                            var reslut = Connect();
                            if (!reslut)
                                goto End;
                            break;
                        }
                    case "q":
                        goto End;
                    case "quit":
                        goto End;
                }
            }
        End:
            Console.WriteLine("*                   结束                      *");
        }
        private static bool Connect()
        {
            string ip;
            int port;

            while (true)
            {
                Console.Write("Ip>");
                ip = Console.ReadLine();
                if (ip.IsIp())
                {
                    break;
                }
            }
            while (true)
            {
                Console.Write("Port>");
                port = Console.ReadLine().SafeToInt();
                if (port.SafeToInt() > 1000)
                {
                    break;
                }
                Console.WriteLine("Port> 请输入大于1000的正整数端口号");
            }
            SocketHelper.Connect(ip, port.SafeToInt());
            while (true)
            {
                Console.Write("UserName>");
                var username = Console.ReadLine();
                Console.Write("Passsword>");
                var password = Console.ReadLine();
                if (username == password)
                    break;
            }
            return true;
        }
        private string ReadLine()
        {
        Input:
            var commend = Console.ReadLine();
            if (string.IsNullOrEmpty(commend)) goto Input;
            switch (commend)
            {
                case "q":
                    goto End;
                case "quit":
                    goto End;
                default:
                    return commend;
            }

        End:
            Console.Clear();
            return "Exit";
        }

    }
}
